package com.springboot.utils;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTCreator;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;

import java.util.Calendar;
import java.util.Map;

public class JwtUtils {

    private static final String SING = "@WWQss232";

    /**
     * 生成token
     */
    public static String getToken(Map<String,String> map){
        Calendar instance = Calendar.getInstance();
//        instance.add(Calendar.DATE, 7);
        instance.add(Calendar.SECOND, 60);
        //创建jwtBuilder
        JWTCreator.Builder builder = JWT.create();
        //自定义参数
        for(String key:map.keySet()){
            builder.withClaim(key, map.get(key));
        }
//        map.forEach((k,v)->{
//            builder.withClaim(k, v);
//        });
        //设置过期时间及签名
        String token = builder.withExpiresAt(instance.getTime())
                .sign(Algorithm.HMAC256(SING));
        return token;
    }

    /**
     * 解析token
     */
    public static DecodedJWT verify(String token){
       return JWT.require(Algorithm.HMAC256(SING)).build().verify(token);
    }

}
